package com.learning.jquizenglish.screen.settings;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.MainActivity;
import com.learning.jquizenglish.common.preference.PreferenceFragment;
import com.learning.jquizenglish.common.preference.SettingsPrefference;
import com.learning.jquizenglish.common.preference.SettingsPrefference.FontSize;


public class SettingScreen extends PreferenceFragment implements OnSharedPreferenceChangeListener {

	private ArrayList<String> mListFontSize;
	
	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		addPreferencesFromResource(R.xml.config);
		String[] list = getActivity().getResources().getStringArray(R.array.fontsize);
		mListFontSize = new ArrayList<String>(Arrays.asList(list));
	}
	
	@Override
	public void onResume() {
		getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		super.onResume();
	}
	
	@Override
	public void onPause() {
		getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		super.onPause();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		//IT NEVER GETS IN HERE!
        if (key.equals("question_font_size"))
        {
            // Set summary to be the user-description for the selected value
            Preference exercisesPref = findPreference(key);
            String value = sharedPreferences.getString(key, mListFontSize.get(1));
            exercisesPref.setSummary(value);
            SettingsPrefference.getInstance().setQuestionFontSize(convertStringToFontSize(value));
        }
        if (key.equals("flashcard_font_size")) {
        	// Set summary to be the user-description for the selected value
            Preference exercisesPref = findPreference(key);
            String value = sharedPreferences.getString(key, mListFontSize.get(1));
            exercisesPref.setSummary(value);
            SettingsPrefference.getInstance().setFlashCardFontSize(convertStringToFontSize(value));
		}
        if (key.equals("showterm")) {
        	// Set summary to be the user-description for the selected value
            boolean value = sharedPreferences.getBoolean(key,true);
            SettingsPrefference.getInstance().setFlashCardShowTerm(value);
		}
        if (key.equals("showdef")) {
        	// Set summary to be the user-description for the selected value
            boolean value = sharedPreferences.getBoolean(key,false);
            SettingsPrefference.getInstance().setFlashCardShowDefinition(value);
		}
        if (key.equals("fullscreen")) {
        	// Set summary to be the user-description for the selected value
            boolean value = sharedPreferences.getBoolean(key,false);
            SettingsPrefference.getInstance().setAppFullScreen(value);
            ((MainActivity)getActivity()).updateFullscreenStatus();
		}
	}
	
	public FontSize convertStringToFontSize(String font){
		if (font.equals(mListFontSize.get(0))) {
			return FontSize.SMALL;
		}else if (font.equals(mListFontSize.get(1))) {
			return FontSize.NORMAL;
		}else if (font.equals(mListFontSize.get(2))) {
			return FontSize.BIG;
		}else{
			return FontSize.NORMAL;
		}
	}
}
